<template>
  <div class="ie-page">
    <div class="main">
      <div :class="['main-container--left', { mounted }]"></div>
      <div class="main-container--right">
        <n-form
          :label-width="80"
          :model="formValue"
          :rules="formRules"
          size="medium"
          ref="formRef"
        >
          <n-form-item>
            <h1>登录 行政调解管理系统</h1>
          </n-form-item>
          <n-form-item
            label="账号"
            style="--label-font-size: 16px;"
            path="username"
            key="username"
          >
            <n-input
              style="--height: 54px;"
              v-model:value="formValue.username"
              size="large"
            />
          </n-form-item>
          <n-form-item
            label="密码"
            style="--label-font-size: 16px;"
            path="password"
            key="password"
          >
            <n-input
              style="--height: 54px;"
              v-model:value="formValue.password"
              type="password"
              @keyup.enter="login"
              show-password-on="click"
              size="large"
            />
          </n-form-item>
          <n-row :gutter="[0, 32]">
            <n-col :span="24">
              <n-checkbox v-model:checked="formValue.remember_me">记住登陆状态</n-checkbox>
            </n-col>
            <n-col :span="24">
              <n-button
                size="large"
                style="height: 54px;"
                color="#4B5EFF"
                @click="login"
                block
              >登&nbsp;陆</n-button>
            </n-col>
          </n-row>
        </n-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useNotification } from 'naive-ui'

import { useStore } from '/@/store'
import { useApi } from '/@/api'

const formRef = ref()
const formInitValue = {
  username: null,
  password: null,
  remember_me: true
}
const formValue = ref({ ...formInitValue  })
const formRules = {
  username: { required: true, message: '账号不可为空' },
  password: { required: true, message: '密码不可为空' }
}

const store = useStore()
const router = useRouter()
const notify = useNotification()

function login() {
  formRef.value.validate(errors => {
    if (!errors) {
      useApi()
        .post('login', formValue.value)
        .then(res => {
          if (res.code === 0) {
            store.setUser(res.data)
            notify.success({
              content: '登录成功',
              meta: '即将跳到首页',
              duration: 5e3,
              onAfterEnter: () => {
                router.push('/')
              }
            })
          }
        })
    }
  })
}

if (store.isLogined) {
  router.push('/')
}

const mounted = ref(false)

onMounted(() => {
  setTimeout(() => mounted.value = true, 0)
})
</script>

<style lang="less">
.main {
  display: flex;
  height: 100%;

  &-container--left {
    flex: 0.4;
    position: relative;
    overflow: hidden;

    &:before {
      content: '';
      background-image: url('/static/image/login-banner.png');
      background-repeat: no-repeat;
      background-size: cover;
      background-position: 0 50%;
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: -40px;
      transition: transform 4s ease .3s
    }

    &:after {
      content: '';
      // background: #D8D8D8;
      // opacity: 0.3;
      // background-image: linear-gradient(46deg, #2E3CB3 0%, #222653 100%);
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      // transition: opacity 5s ease .3s;
    }

    &.mounted {

      &:before {
        transform: translateX(-40px);
      }

      &:after {
        // opacity: 0.88;
      }
    }
  }

  &-container--right {
    flex: 0.6;

    .n-form {
      margin: 22.2vh auto 0;
      max-width: 400px;
    }
  }
}
</style>